home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7730 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const char??
  5. Date: 27 Feb 1996 18:52:36 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb27115236@qcd.lanl.gov>
  8. References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com>
  9.     <TANMOY.96Feb20095538@qcd.lanl.gov>
  10.     <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: c2a192@ugrad.cs.ubc.ca's message of 25 Feb 1996 12:09:20 -0800
  15.  
  16. In article <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
  17. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  18. <snip>
  19. KK:  >(There is a related issue that the function may also change *x by
  20. KK:  >using the lvalue *(char*)x. That, and similar tricks, are so bad
  21. KK:  >programming practices, that I won't even comment on them!)
  22. KK: 
  23. KK: Hmm. Is that not a bit of an error, since the const modifier is
  24. being dropped 
  25. KK: in the cast? C allows you to only safely convert pointers of any
  26. type to void * 
  27. KK: and back to the same type. I will have to check references whether
  28. or not that 
  29. KK: "type" is so restrictive as to include any modifiers like const
  30. and volatile. 
  31.  
  32. Actually, the C standard guarantees that a pointer to any object type
  33. (including incomplete object type) can be converted to any other
  34. object type if one uses a cast. So (char*)x is valid.
  35.  
  36. It then states that the resultant pointer may not be valid if the
  37. alignment requirements are not met. As char has the least alignment
  38. requirement (and in any conversion from a const to a non-const type),
  39. (char*)x is a valid pointer: so it can be used without invoking
  40. undefined behaviour. Thus for the expression (char*)x+0, no further
  41. argumentation would have been necessary. 
  42.  
  43. For *(char*)x, one needs one more step. The standard says that an
  44. object may be accessed only through an lvalue of a compatible type,
  45. _or_ an lvalue of a character type. So, use of *(char*)x is valid.
  46.  
  47. KK: 
  48. KK: In any case, the cast does make it explicit that you are trying to
  49. voluntarily 
  50. KK: break the const. But in your function declaration you promised
  51. that you would 
  52. KK: not touch the object---why go through the bother of making that
  53. promise to the 
  54. KK: caller, when you intend to violate it?
  55.  
  56. That is precisely what I meant when I said that I do not want to
  57. discuss it further. By the way, if x is a pointer to the first
  58. character of a string, *strchr(x,*x) is another way of writing the
  59. same lvalue as *(char*)x ... but such tricks are really terrible.
  60.  
  61. KK: 
  62. KK:  >An object _defined_ a (i.e. an object whose storage was allocated by a
  63. KK:  >declaration specifying) const, cannot be portably changed by the
  64. KK:  >program. A compiler can assume it's value won't change unless it is
  65. KK:  >also volatile: in which case, even though the program cannot write to
  66. KK:  >it, the compiler has to assume that its value can change due to unkwon
  67. KK:  >reasons. 
  68. KK: 
  69. KK: A read-only device status register would be an example of such a "volatile
  70. KK: const" beast, no?
  71.  
  72. Yes, provided the status register is memory mapped :-)
  73.  
  74. Cheers
  75. Tanmoy
  76. --
  77. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  78. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  79. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  80. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  81. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  82. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  83.